// This kernel perform vectorized floating-point addition/multiplication
// to demonstrate how parallel processing can accelerate computation
// 2020.06.17 by wangdong@bjtu.edu.cn
//#include "ap_int.h"
// #include <stdio.h> 


#include "custypedef.h"


//--------------------- Baseline -----------------------//
//#define BUFFER_DEPTH 4096
extern "C" {
void featureRead(
            	DATA_TYPE *A_in,
				uint data_num,
				REFERENCE_STREAM(k2k<vec_type>, 16, img_channels)
				)
{
	#pragma HLS INTERFACE m_axi port = A_in  offset = slave bundle = gmem0 //num_read_outstanding=1 max_read_burst_length=1
	#pragma HLS INTERFACE axis  port = img_channels depth=16

	DATA_TYPE img_ori_0[BUFFER_DEPTH * 2];
	DATA_TYPE img_ori_1[BUFFER_DEPTH * 2];
	DATA_TYPE img_ori_2[BUFFER_DEPTH * 2];
	bool flag = 0;
	
	volatile int Rdcnt = 0;
	for(uint k = 0; k<DATA_SIZE_M; k++){
		
		for(int i = -(VEC_SIZE) + 1; i < DATA_SIZE_H - DATA_SIZE_K + 1; i++)
		{
			for(int nn = 0; nn < DATA_SIZE_N; nn++){
				for(uint j = 0; j< DATA_SIZE_W; j++){
                    #pragma HLS PIPELINE II=1 rewind
					img_ori_2[j + nn * DATA_SIZE_W + flag * BUFFER_DEPTH] = A_in[j + (i + 2) * DATA_SIZE_W + nn * DATA_SIZE_W * DATA_SIZE_H];
				}
			}
			// if(i >= 0){
			for(uint s = 0; s<DATA_SIZE_W - DATA_SIZE_K + 1; s++){
				for(int nn = 0; nn < DATA_SIZE_N; nn++){ 
					for(uint s2 = 0; s2 < DATA_SIZE_K; s2++){
					#pragma HLS PIPELINE II=1 rewind
						k2k<vec_type> _trans_img_oris;
						DATA_TYPE tmp0, tmp1, tmp2;
						tmp0 = img_ori_0[s + s2 + nn * DATA_SIZE_W + flag * BUFFER_DEPTH];
						tmp1 = img_ori_1[s + s2 + nn * DATA_SIZE_W + flag * BUFFER_DEPTH];
						tmp2 = img_ori_2[s + s2 + nn * DATA_SIZE_W + flag * BUFFER_DEPTH];

						img_ori_0[s + s2 + nn * DATA_SIZE_W + (!flag) * BUFFER_DEPTH] = tmp1;
						img_ori_1[s + s2 + nn * DATA_SIZE_W + (!flag) * BUFFER_DEPTH] = tmp2;
						if(i>=0){
							_trans_img_oris.data(GET_BIT(DATA_TYPE)-1, 0) = tmp0;
							_trans_img_oris.data(GET_BIT(DATA_TYPE)*2-1, GET_BIT(DATA_TYPE)) = tmp1;
							_trans_img_oris.data(GET_BIT(DATA_TYPE)*3-1, GET_BIT(DATA_TYPE)*2) = tmp2;
							img_channels.write(_trans_img_oris);
						}

						
						++Rdcnt;
					}
				}
			}
			flag = !flag;
			// }
			// for (int ll = 0;ll < DATA_SIZE_W * DATA_SIZE_N;ll++)
			// {
			// 	img_ori_0[ll] = img_ori_1[ll];
			// }
			// for (int ll = 0; ll < DATA_SIZE_W * DATA_SIZE_N; ll++)
			// {
			// 	img_ori_1[ll] = img_ori_2[ll];
			// }
		}
	}
}

}
